home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / The Crash Manager / "source" / preferences.p < prev    next >
Encoding:
Text File  |  1996-06-30  |  3.5 KB  |  123 lines  |  [TEXT/CWIE]

  1. unit preferences;
  2.  
  3. interface
  4.  
  5. uses
  6.     Types,
  7.     Folders,
  8.     Script,
  9.     Processes,
  10.     Files;
  11.  
  12. procedure LoadPreferences(thePreferences:Ptr;sizeOfPreferences:longint;preferencesName:Str32);
  13. procedure SavePreferences(thePreferences:Ptr;sizeOfPreferences:longint;preferencesName:Str32);
  14. function ApplicationCreator:OSType;
  15.  
  16. implementation
  17.  
  18. function ApplicationCreator:OSType;
  19. var
  20.     thisProcess:ProcessSerialNumber;
  21.     theProcessInfo:ProcessInfoRec;
  22.     processError:OSErr;
  23.     temp:OSType;
  24. begin
  25.     temp:='Err.';
  26.     theProcessInfo.processInfoLength:=SizeOf(ProcessInfoRec);
  27.     theProcessInfo.processName:=nil;
  28.     theProcessInfo.processAppSpec:=nil;
  29.     thisProcess.highLongOfPSN:=0;
  30.     thisProcess.lowLongOfPSN:=kCurrentProcess;
  31.     processError:=GetProcessInformation(thisProcess,theProcessInfo);
  32.     if processError=noErr then
  33.         temp:=theProcessInfo.processSignature;
  34.     ApplicationCreator:=temp;
  35. end;
  36.  
  37. procedure LoadPreferences(thePreferences:Ptr;sizeOfPreferences:longint;preferencesName:Str32);
  38. const
  39.     fnfErr=-43;
  40. var
  41.     theError:OSErr;
  42.     prefVolRef,prefFileRef:integer;
  43.     prefDirID,jcgDirID:longint;
  44.     jcgDirSpec,prefFileSpec:FSSpec;
  45.     theCIPB:CInfoPBRec;
  46.     jcgDirName:Str32;
  47. begin
  48.     jcgDirName:='jcg';
  49.     theError:=FindFolder(kOnSystemDisk,kPreferencesFolderType,true,prefVolRef,prefDirID);
  50.     if theError=noErr then begin
  51.         theError:=FSMakeFSSpec(prefVolRef,prefDirID,jcgDirName,jcgDirSpec);
  52.         if (theError=noErr) or (theError=fnfErr) then begin
  53.             if theError=fnfErr then
  54.                 theError:=FSpDirCreate(jcgDirSpec,smSystemScript,jcgDirID)
  55.             else begin
  56.                 theCIPB.ioCompletion:=nil;
  57.                 theCIPB.ioNamePtr:=@jcgDirName;
  58.                 theCIPB.ioVRefNum:=prefVolRef;
  59.                 theCIPB.ioDrDirID:=prefDirID;
  60.                 theCIPB.ioFDirIndex:=0;
  61.                 theError:=PBGetCatInfoSync(@theCIPB);
  62.                 jcgDirID:=theCIPB.ioDrDirID;
  63.             end;
  64.             if theError=noErr then begin
  65.                 theError:=FSMakeFSSpec(prefVolRef,jcgDirID,preferencesName,prefFileSpec);
  66.                 if (theError=noErr) or (theError=fnfErr) then begin
  67.                     if theError=fnfErr then begin
  68.                         theError:=FSpCreate(prefFileSpec,
  69.                                             ApplicationCreator,
  70.                                             'pref',
  71.                                             smSystemScript);
  72.                         if theError=noErr then
  73.                             SavePreferences(thePreferences,sizeOfPreferences,preferencesName);
  74.                     end else
  75.                         if theError=noErr then begin
  76.                             theError:=FSpOpenDF(prefFileSpec,fsCurPerm,prefFileRef);
  77.                             if theError=noErr then begin
  78.                                 theError:=FSRead(prefFileRef,sizeOfPreferences,thePreferences);
  79.                                 theError:=FSClose(prefFileRef);
  80.                             end;
  81.                         end;
  82.                 end;
  83.             end;
  84.         end;
  85.     end;
  86. end;
  87.  
  88. procedure SavePreferences(thePreferences:Ptr;sizeOfPreferences:longint;preferencesName:Str32);
  89. var
  90.     jcgDirName:Str32;
  91.     theError:OSErr;
  92.     prefVolRef,prefFileRef:integer;
  93.     prefDirID,jcgDirID:longint;
  94.     jcgDirSpec,prefFileSpec:FSSpec;
  95.     theCIPB:CInfoPBRec;
  96. begin
  97.     jcgDirName:='jcg';
  98.     theError:=FindFolder(kOnSystemDisk,kPreferencesFolderType,true,prefVolRef,prefDirID);
  99.     if theError=noErr then begin
  100.         theError:=FSMakeFSSpec(prefVolRef,prefDirID,jcgDirName,jcgDirSpec);
  101.         if theError=noErr then begin
  102.             theCIPB.ioCompletion:=nil;
  103.             theCIPB.ioNamePtr:=@jcgDirName;
  104.             theCIPB.ioVRefNum:=prefVolRef;
  105.             theCIPB.ioDrDirID:=prefDirID;
  106.             theCIPB.ioFDirIndex:=0;
  107.             theError:=PBGetCatInfoSync(@theCIPB);
  108.             jcgDirID:=theCIPB.ioDrDirID;
  109.             if theError=noErr then begin
  110.                 theError:=FSMakeFSSpec(prefVolRef,jcgDirID,preferencesName,prefFileSpec);
  111.                 if theError=noErr then begin
  112.                     theError:=FSpOpenDF(prefFileSpec,fsRdWrPerm,prefFileRef);
  113.                     if theError=noErr then begin
  114.                         theError:=FSWrite(prefFileRef,sizeOfPreferences,thePreferences);
  115.                         theError:=FSClose(prefFileRef);
  116.                     end;
  117.                 end;
  118.             end;
  119.         end;
  120.     end;
  121. end;
  122.  
  123. end.